package com.tang.qzzp.task;

import com.tang.qzzp.service.TextService;
import com.tang.qzzp.utils.RedisUtil;
import com.tang.qzzp.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 文本类定时任务
 *
 * @author 唐海清
 */
@Component
@Slf4j(topic = "TextTask")
public class TextTask {
    @Resource
    private TextService textService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 每天上午10:15查询一次文本条数
     */
    @Scheduled(cron = "0 15 10 ? * *")
    public void selectTextCounts() {
        log.info("执行定时任务：查询一次文本条数");
        int count = (int)textService.count();
        log.info("mysql文本条数：{}", count);
        int redisTextCounts = (int) redisUtil.get(StringUtil.TEXT_PREFIX + "_counts");
        log.info("redis缓存文本条数为：{}", redisTextCounts);
        if (count != redisTextCounts) {
            log.info("执行更新操作");
            redisUtil.set(StringUtil.TEXT_PREFIX + "_counts", count);
        }
    }
}
